package cz.muni.fi.heathtow.web;

import cz.muni.fi.heathtow.web.pages.FlightsView;
import cz.muni.fi.heathtow.web.pages.StewardView;
import cz.muni.fi.heathtow.web.pages.HomePage;
import cz.muni.fi.heathtow.web.pages.AircraftView;
import cz.muni.fi.heathtow.web.pages.DestinationView;
import cz.muni.fi.heathtow.web.auth.SecureWicketAuthenticatedWebSession;
import cz.muni.fi.heathtow.web.errorpages.AccessDeniedPage;
import cz.muni.fi.heathtow.web.errorpages.InternalErrorPage;
import cz.muni.fi.heathtow.web.errorpages.PageExpiredErrorPage;
import cz.muni.fi.heathtow.web.pages.JSONRequests;
import cz.muni.fi.heathtow.web.pages.SignIn;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.settings.IExceptionSettings;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

/**
 * Application object for your web application. If you want to run this
 * application without deploying, run the Start class.
 *
 * @see cz.muni.fi.heathtow.web.Start#main(String[])
 */
public class WicketApplication extends AuthenticatedWebApplication {
    
    boolean isInitialized = false;
    
    /**
     * @see org.apache.wicket.Application#getHomePage()
     */
    @Override
    public Class<? extends WebPage> getHomePage()
    {
        return HomePage.class;
    }

    /**
     * @see org.apache.wicket.Application#init()
     */
    @Override
    public void init()
    {
        if (!isInitialized) {
            super.init();



            getDebugSettings().setAjaxDebugModeEnabled(false);   
            getComponentInstantiationListeners().add(new SpringComponentInjector(this));

            mountPage("/home", HomePage.class);
            mountPage("/Aircraft", AircraftView.class);
            mountPage("/Destinations", DestinationView.class);
            mountPage("/Flights", FlightsView.class);
            mountPage("/Stewards", StewardView.class);
            mountPage("/Signin", SignIn.class);
            mountPage("/JSONRequests", JSONRequests.class);


            getApplicationSettings().setPageExpiredErrorPage(PageExpiredErrorPage.class);
            getApplicationSettings().setAccessDeniedPage(AccessDeniedPage.class);
            getApplicationSettings().setInternalErrorPage(InternalErrorPage.class);

            // show internal error page rather than default developer page
            getExceptionSettings().setUnexpectedExceptionDisplay(IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE); 
            isInitialized = true;
        }
    }    

    @Override
    protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
        return SecureWicketAuthenticatedWebSession.class;
    }

    @Override
    protected Class<? extends WebPage> getSignInPageClass() {
        return SignIn.class;
    }
}
